package com.szosen.device.jianhuyi;
public class Tools_Findandone {
	/**
	 * 血压状态
	 * 
	 * @param data
	 *            十六进制数据
	 * @return 1 正在测试;6 为袖带过松，7：为 漏气，
	 *         9：信号弱，10：信号弱，11，过分运动，12：过压，14：漏气，15：系统错误，19：为 超时，其它值：为其它错误
	 */
	public static String getStatus(String data) {
		// 、0XEE,0X35,0x10,01,00,0X55,0XCC 正在测试 2、0XEE,0X35,0x10,00,XX,0X55,0XCC
		byte[] b = StringUtils.HexToByteArr(data);
		if (b[3] == 0x01) {
			return "正在测试";
		} else if (b[3] == 0x00) {
			switch (b[4]) {
			case 0x06:
				return "袖带过松,请重试";
			case 0x07:
				return "漏气,请重试";
			case 0x09:
				return "信号弱";
			case 0x10:
				return "信号弱";
			case 0x11:
				return "过分运动";
			case 0x12:
				return "过压";
			case 0x14:
				return "漏气";
			case 0x15:
				return "系统错误";
			case 0x19:
				return "超时";
			default:
				return "其他错误";
			}
		}
		return "正在测试";
	}

	/**
	 * 获取血压
	 * 
	 * @param data
	 *            16进制的血压数据字符串
	 * @return String[]={"收缩压","舒张压","平均压","脉率"}
	 */
	public static String[] getBlood(String data) {
		int start = data.indexOf("EE351002");
		int end = data.indexOf("55CC");

		String str = data.substring(start + 8, end);

		byte[] cc = StringUtils.HexToByteArr(str);

		// 收缩压

		String sp = StringUtils.HexToInt(GD(str.substring(0, 4))) + "";
		String dp = StringUtils.HexToInt(GD(str.substring(4, 8))) + "";
		String mp = StringUtils.HexToInt(GD(str.substring(8, 12))) + "";
		String pl = StringUtils.HexToInt(GD(str.substring(12, 16))) + "";
		String[] result = new String[4];
		result[0] = sp;
		result[1] = dp;
		result[2] = mp;
		result[3] = pl;
		return result;
	}

	/**
	 * 获取血氧
	 * 
	 * @param data
	 *            16进制字符串
	 * @return s=new String[]{血氧浓度，血氧心率}
	 */
	public static String[] getXueyang(String data) {
		// 0XEE,0X35,0x20,XX,XX,0X55,0XCC 其 中：XX 依次为：血氧浓度，血氧心率
		int start = 6;// "EE3520"
		int end = data.indexOf("55CC");
		String mb = data.substring(start, end);
		if (mb.length() == 4) {
			String sp = StringUtils.HexToInt(mb.substring(0, 2)) + "";
			String dp = StringUtils.HexToInt(mb.substring(2, 4)) + "";
			String[] result = new String[2];
			result[0] = sp;
			result[1] = dp;
			return result;
		} else
			System.out.println(mb.length());
		return null;
	}

	/** 高位在前，低位在后 */
	private static String GD(String data) {
		byte[] bb = StringUtils.HexToByteArr(data);
		byte b;
		b = bb[0];
		bb[0] = bb[1];
		bb[1] = b;
		String ndata = StringUtils.ByteArrToHex(bb);
		return ndata;
	}

	/**
	 * 获取温度
	 * 
	 * @param data
	 *            16进制的字符串
	 * @return String 温度值
	 */
	public static String getWendu(String data) {
		// 0XEE,0X35,0x30,XX,XX,0X55,0XCC
		int start = 6;// 0XEE,0X35,0x30
		int end = data.indexOf("55CC");
		String md = data.substring(start, end);
		String gd = GD(md);
		System.out.println(gd);
		gd=gd.replaceAll(" ", "");
		byte[] bb = StringUtils.HexToByteArr(gd);
//		double b = bb[0] * 255 + bb[1];
//		double wendu = b / 10;
//		return wendu + "";
		int n=StringUtils.HexToInt(gd);
		String nn=n+"";
		if(nn.length()==3){
			double dd=n;
			double ddd=dd/10;
			 return ddd+"";
		}
		else return n+"";
	}

	/**
	 * 获取胎心
	 * 
	 * @param data
	 *            16进制胎心数据
	 * @return result=new int[]{"心率","信号"}
	 * */
	public static int[] getTaixin(String data) {
		// 0XEE,0X35,0x40,XX,XX,0X55,0XCC 其 中前一个 XX,为胎心率，0 没有检测到.每二个 XX
		// 为状态：1：信号差，2：信号一般，3：信 号好
		int start = 6;
		int end = data.indexOf("55CC");
		String md = data.substring(start, end);
		byte[] bb = StringUtils.HexToByteArr(md);
		int xinlv = bb[0];
		int status = bb[1];
		int[] result = new int[2];
		result[0] = xinlv;
		result[1] = status;
		return result;
	}

	public static int getFrom(String data) {
		byte[] bb = StringUtils.HexToByteArr(data);
		int who = 20;
		switch (bb[2]) {
		case 0x10:
			who = 10;
			break;
		case 0x20:
			who = 20;
			break;
		case 0x30:
			who = 30;
			break;
		case 0x40:
			who = 40;
			break;
		default:
			break;
		}
		
		return who;

	}

	public static void main(String[] args) {
		String data = "EE3510022d015C030302720155CC";
		String dd[] = getBlood(data);
		for (String d : dd) {
			System.out.println(d);
		}
		double s=365/10;
		System.out.println(s);
	}
}
